package com.code.exam.service.impl;


import com.code.exam.common.result.PageResult;
import com.code.exam.dto.TiIdsDTO;
import com.code.exam.dto.ExamCreateDTO;

import com.code.exam.dto.ExamPageQueryDTO;
import com.code.exam.entity.Exam;
import com.code.exam.mapper.ExamMapper;
import com.code.exam.service.ExamService;
import com.code.exam.vo.AddTiIdsVO;
import com.code.exam.vo.EPageResultVO;
import com.code.exam.vo.ExamAllquestionsVO;
import com.code.exam.vo.QuestionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {


    @Autowired
    private ExamMapper examMapper;

    public Exam examcreate(ExamCreateDTO examCreateDTO){
        Exam exam = new  Exam();
        BeanUtils.copyProperties(examCreateDTO,exam);
        if(exam.getStatus() == null)
            exam.setStatus("UPCOMING");
        examMapper.examcreate(exam);
        return exam;
    }

    public Exam updateStatus(Long examId,String status){
        Exam exam = examMapper.getById(examId);
        if (exam == null) {
            throw new RuntimeException("考试不存在，ID: " + examId);
        }
        else if(exam.getStatus().equals("ENDED")){
            throw  new RuntimeException("注意：不允许将已结束考试修改");
        }
        else {
            exam.setStatus(status);
            examMapper.updateStatus(examId,status);
        }
        return exam;
    }

    public PageResult<EPageResultVO> pageQuery(ExamPageQueryDTO examPageQueryDTO){

        //计算offset和limit
        int offset = (examPageQueryDTO.getPage() - 1) * examPageQueryDTO.getPageSize();
        int limit = examPageQueryDTO.getPageSize();

        examPageQueryDTO.setOffset(offset);
        examPageQueryDTO.setLimit(limit);

        LocalDateTime startTimeFrom = examPageQueryDTO.getStartFrom() != null ?
                examPageQueryDTO.getStartFrom().atStartOfDay() : null;

        LocalDateTime startTimeTo = examPageQueryDTO.getStartTo() != null ?
                examPageQueryDTO.getStartTo().plusDays(1).atStartOfDay() : null;

        //查询总数
        Long total = examMapper.countByCondition(examPageQueryDTO,startTimeFrom,startTimeTo);


        //查询当前页数据
        List<EPageResultVO> records = examMapper.selectByCondition(examPageQueryDTO,startTimeFrom,startTimeTo);


        //封装并返回

        return new  PageResult<>(total,examPageQueryDTO.getPage(),examPageQueryDTO.getPageSize(),records);

    }

    public String getStatus(Long examId){
        String status = examMapper.getStatusById(examId);
        return status;
    }


    public AddTiIdsVO addQuestions(Long examId, TiIdsDTO addTiIdsDTO){
        AddTiIdsVO addTiIdsVO = new AddTiIdsVO();
        addTiIdsVO.setExamId(examId);
        List questionIds = addTiIdsDTO.getQuestionIds();
        int addedcount = examMapper.addQuestions(examId,questionIds);
        addTiIdsVO.setAddedCount(addedcount);
        return addTiIdsVO;
    }

    public int removeTi(Long examId,TiIdsDTO tiIdsDTO){
        List questionIds = tiIdsDTO.getQuestionIds();
        int removedCount = examMapper.removeTi(examId,questionIds);
        return removedCount;
    }

    public ExamAllquestionsVO getAllQuestions(Long examId){

        //根据考试Id查询所有题目
        List<QuestionVO> records = examMapper.getAllQuestions(examId);


        //获取考试标题
        String title = examMapper.getTitle(examId);

        //封装结果
        ExamAllquestionsVO examAllquestionsVO = ExamAllquestionsVO.builder()
                .examId(examId)
                .title(title)
                .records(records)
                .build();

        return examAllquestionsVO;
    }
}
